<?php

namespace App\Http\Controllers\admin;
use Crypt;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class userController extends Controller
{
    public function add(){
    	return view('admin.user.add');
    }
  //**************************************增加***********
    public function insert(Request $request)
    {
      //dd($request -> all());
      // if(!$request -> input('name'))
      // {
      // 	return back() -> with(['name'=>'用户名不能为空']);
      //
      // }
      $this -> validate($request,[
      	   'name'=>'required',
      	   'password'=>'required',
      	   'repassword'=>'required|same:password',
      	   'email'=>'required|email',
      	   'phone'=>'max:11|min:11'
      	                         ],[
            'name.required'=>'用户名不能为空',
            'password.required'=>'密码不能为空',
            'repassword.required'=>'确认密码不能为空',
            'repassword.same'=>'2次密码不一致',
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不对',
            'phone.max'=>'手机号必须是11位的',
            'phone.min'=>'手机号必须是11位的',
      	                         ]
      	);
           $data = $request -> except('_token','repassword');
           //密码加密
           $data['password'] = Crypt::encrypt($data['password']);
         
           date_default_timezone_set('PRC');
           $data['remember_token'] = str_random(70);
            $time = date("Y-m-d H:i:s");
            $data['created_at'] = $time;
            $data['userName'] = rand(10000000,99999999);
          $res = DB::table('users') -> insert($data);
           if($res)
           {
           	return redirect('/admin/user/menu') -> with(['info'=>'添加成功']);
           }else{
           	return redirect('/admin/user/add') -> with(['info' => '添加失败']);
           }

    }
    //查看***************************************************
     public function menu(Request $request)
     {  
     	$num = $request ->input('num',10);

     	$data = DB::table('users') 
     	     ->where(function($query)use($request){
     	     	$query->where('name','like','%'.$request->input('keyword').'%');
     	     })
     	     
      	     -> paginate($num);
              //dd($request -> all());
     	return view('admin.user.menu',['data'=>$data,'request'=> $request -> all()]);
     }
     //修改***********************************************
     public function alter($id)
     {
      $data = DB::table('users')-> where('id',$id)-> first();
      //dd($data);
       return view('admin.user.alter',['data' => $data]);
     }
     public function update(Request $request)

     {
        $this -> validate($request,[
        	  'phone' => 'required',
        	  'name' => 'required',
        	  'email'=> 'required|email'
        	],[
        	  'name.required'=>'用户名不能为空',
        	  'email.required'=> '邮箱不能为空',
        	  'email.email'=> '邮箱格式不对',
        	  'phone.required'=>'联系电话不能为空'
        	]);
       $data = $request -> except('_token','id');
       //dd($data);
   
       //执行
      $res = DB::table('users') ->where('id',$request->input('id')) -> update($data);
       if($res)
       {
       	return redirect('/admin/user/menu')->with(['info' => '修改成功']);
       }else{
       	return back() -> with(['info' => '修改失败']);
       }
     }
     //删除*****************************************************
     public function delete($id)
     {
     	//return $id;
     	$res = DB::table('users') -> delete($id);
     	//判断
     	if($res)
     	{
     		return redirect('/admin/user/menu') -> with(['info' => '删除成功']);
     	}else{
     		return back() -> with(['info' => '删除失败']);
     	}
     }
     //***********************************************************
     public function status($id)
     {

     	$res = DB::table('users') -> where('id',$id) ->first();
     	//dd($res);
     	$array = [];
     	if($res -> status == 0)
     	{
            $array = ['status' => '1'];
     	}else{
     		$array = ['status' => '0'];
     	}
       $result = DB::table('users')-> where('id',$id) -> update($array);
       if($result)
       {
       	return redirect('/admin/user/menu');
       }
     }
     //***************************************************************
     public function ajaxName(Request $request)
     {
     	$name = $request -> input('name');
     	//dd($request -> input('id'));
     	//查询
     	$res = DB::table('users')-> where('name',$name) -> first();
        
        if($res)
        {
     	   return response() -> json(1);//1代表失败存在 用户名已经存在；
        }else{

           $res = DB::table('users') -> where('id',$request -> input('id'))
                -> update(['name' => $name]);
           if($res)
           {
           	 return response() -> json(0);//成功
           }else{
           	 return response() -> json(2);//失败
           }
        }

     }  
}
